<?php
/**
 * @author: shwdai@gmail.com
 */
class Template
{
	
	private $_complied_dir = '';
	private $_CI;
	
	static function replace($string)
	{
		return str_replace('\"', '"', $string);
	}
	
	
	function __construct()
	{
		$this->_CI = & get_instance();
		
		/**
		 * Define WEB_ROOT
		 */
		$document_root = $_SERVER['DOCUMENT_ROOT'];
		$docroot = rtrim(str_replace('\\','/',$document_root),'/');
		if(!$docroot) {
			$script_filename = $_SERVER['SCRIPT_FILENAME'];
			$script_filename = str_replace('\\','/',$script_filename);
			$script_name = $_SERVER['SCRIPT_NAME'];
			$script_name = str_replace('\\','/',$script_name);
			$lengthf = strlen($script_filename);
			$lengthn = strlen($script_name);
			$length = $lengthf - $lengthn;
			$docroot = rtrim(substr($script_filename,0,$length),'/');
		}
		$webroot = trim(substr(WWW_ROOT, strlen($docroot)), '\\/');
		define('WEB_ROOT', $webroot ? "/{$webroot}" : '');		
		
	}	
	
	/**
	 * 显示模板
	 * @param string $file
	 * @param mixed $var
	 */
	static public function complie($tFile, $full_path = false)
	{				
		$complied_dirname = '_complied/';
		$complied_dir = APPPATH . 'views/' . $complied_dirname;	
		$newFile = 	str_ireplace(array('/','\\'),'_',substr(md5($tFile),0,6) . "_{$tFile}.php");
		$cFile = $complied_dir . $newFile;
		$tFile = APPPATH . "views/{$tFile}.php";
		
		if(!file_exists($tFile))
		{
			echo 'Template File Not Exists!';
			return;
		}				
		
		self::_parse($tFile, $cFile);	
		if($full_path)
		{
			return $complied_dir . $newFile;
		}	
		return $complied_dirname . $newFile ;
	}
	
	private static function _parse($tFile, $cFile) {
				
	    if(file_exists($cFile) && @filemtime($tFile) < @filemtime($cFile))
		{			
			return $cFile;
		}
		
	    $fileContent = false;
	
	    if(!($fileContent = file_get_contents($tFile)))
	        return false;
	
		$fileContent = preg_replace( '/^(\xef\xbb\xbf)/', '', $fileContent ); //EFBBBF, Remove BOM
	    $fileContent = preg_replace("/\<\!\-\-\s*\\\$\{(.+?)\}\s*\-\-\>/ies", "Template::replace('<?php \\1; ?>')", $fileContent);
	    $fileContent = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\\\ \-\'\,\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/s", "<?php echo \\1; ?>", $fileContent);
	    $fileContent = preg_replace("/\\\$\{(.+?)\}/ies", "Template::replace('<?php echo \\1; ?>')", $fileContent);
	    $fileContent = preg_replace("/\<\!\-\-\s*\{else\s*if\s+(.+?)\}\s*\-\-\>/ies", "Template::replace('<?php } else if(\\1) { ?>')", $fileContent);
	    $fileContent = preg_replace("/\<\!\-\-\s*\{elif\s+(.+?)\}\s*\-\-\>/ies", "Template::replace('<?php } else if(\\1) { ?>')", $fileContent);
	    $fileContent = preg_replace("/\<\!\-\-\s*\{else\}\s*\-\-\>/is", "<?php } else { ?>", $fileContent);
	
	    for($i = 0; $i < 5; ++$i) {
	        $fileContent = preg_replace("/\<\!\-\-\s*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\s*\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/loop\}\s*\-\-\>/ies", "Template::replace('<?php if(is_array(\\1)){foreach(\\1 AS \\2=>\\3) { ?>\\4<?php }}?>')", $fileContent);
	        $fileContent = preg_replace("/\<\!\-\-\s*\{loop\s+(\S+)\s+(\S+)\s*\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/loop\}\s*\-\-\>/ies", "Template::replace('<?php if(is_array(\\1)){foreach(\\1 AS \\2) { ?>\\3<?php }}?>')", $fileContent);
	        $fileContent = preg_replace("/\<\!\-\-\s*\{if\s+(.+?)\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/if\}\s*\-\-\>/ies", "Template::replace('<?php if(\\1){?>\\2<?php }?>')", $fileContent);
	    }
	
	    //Add for call <!--{include othertpl}-->
	    $fileContent = preg_replace("#<!--\s*{\s*include\s+([^\{\}]+)\s*\}\s*-->#i", '<?php include Template::complie("\\1", true);?>', $fileContent);
		
		@file_put_contents($cFile, $fileContent);

	    return $cFile;
	}

}?>
